package com.example.ymm.modules1.controller.hl;

import com.alibaba.fastjson.JSONArray;
import com.example.ymm.model.CommonResult;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.controller.BaseController;
import com.example.ymm.modules1.entity.hl.dkn.DknProductMapping;
import com.example.ymm.modules1.entity.hl.dkn.DknPurchaseOrder;
import com.example.ymm.modules1.query.hl.DknProductMappingQueryCriteria;
import com.example.ymm.modules1.query.hl.DknPurchaseOrderQueryCriteria;
import com.example.ymm.modules1.service.dkn.DknOrderService;
import com.example.ymm.modules1.service.dto.dkn.DknEditOrderDto;
import com.example.ymm.modules1.service.dto.heng.DknProductMappingDto;
import com.example.ymm.modules1.service.dto.heng.ImportDknProductDto;
import com.example.ymm.modules1.service.dto.ms.MsOrderListQueryCriteria;
import com.example.ymm.modules1.service.hl.DknProductMappingService;
import com.example.ymm.modules1.service.hl.DknPurchaseOrderService;
import com.example.ymm.util.UtilValidate;
import com.fasterxml.jackson.databind.JsonNode;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/hl/heng/dkn/product")
@Tag(name = "hl dkn 产品管理")
@Slf4j
public class DknProductController extends BaseController {

    @Autowired
    DknProductMappingService dknProductMappingService;

    @Autowired
    DknPurchaseOrderService dknPurchaseOrderService;

    @Autowired
    DknOrderService dknOrderService;

    @Schema( name =  "产品列表")
    @PostMapping("/productList")
    public CommonResult productList(@Valid DknProductMappingQueryCriteria criteria, YmPageDto ymPage) {
        try {
            TableList<DknProductMapping> list = dknProductMappingService.list(criteria, ymPage);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Schema( name =  "产品保存")
    @PostMapping("/productSave")
    public CommonResult productSave(DknProductMapping productMapping) {
        try {
            dknProductMappingService.saveOrUpdate(productMapping);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Schema( name =  "产品删除")
    @PostMapping("/productRemove")
    public CommonResult productRemove(DknProductMapping productMapping) {
        try {
            dknProductMappingService.removeById(productMapping);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }



    @Schema( name =  "产品导入")
    @PostMapping("/importDknProduct")
    public CommonResult importDknProduct(@RequestBody ImportDknProductDto dto) {
        try {
            List<String> strings = dknProductMappingService.importDknProduct(dto);
            return CommonResult.success(strings);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Schema( name =  "订单列表")
    @PostMapping("/orderList")
    public CommonResult orderList(@Valid DknPurchaseOrderQueryCriteria criteria, YmPageDto ymPage) {
        try {
            TableList<DknPurchaseOrder> list = dknPurchaseOrderService.list(criteria, ymPage);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Schema( name =  "订单物料信息")
    @PostMapping("/orderItemList")
    public CommonResult orderItemList(String inquiryCode) {
        try {
            JSONArray array = dknPurchaseOrderService.orderItemList(inquiryCode);
            return CommonResult.success(array, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Schema( name =  "查询订单状态")
    @PostMapping("/orderStatus")
    public CommonResult orderStatus(String inquiryCode) {
        try {
            JsonNode jsonNode = dknOrderService.inquiriesOrder(inquiryCode);
            return CommonResult.success(jsonNode, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Schema( name =  "订单取消")
    @PostMapping("/orderCancel")
    public CommonResult orderCancel(DknPurchaseOrder order) {
        try {
            dknPurchaseOrderService.orderCancel(order);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Schema( name =  "订单详情")
    @PostMapping("/orderDetail")
    public CommonResult orderDetail(DknPurchaseOrder order) {
        try {
            JsonNode jsonNode = dknPurchaseOrderService.orderDetail(order);
            return CommonResult.success(jsonNode);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Schema( name =  "查看已下订单之中哪些 没有映射到")
    @PostMapping("/itemNoMapping")
    public CommonResult itemNoMapping(String orderNo) {
        try {
            if(UtilValidate.isEmpty(orderNo)){
                return CommonResult.error("参数非法");
            }
            MsOrderListQueryCriteria criteria=new MsOrderListQueryCriteria();
            criteria.setOrderNo(orderNo);
            List<DknProductMappingDto> list = dknProductMappingService.purchaseOrderNoMappingList(criteria);
            return CommonResult.success(list);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Schema( name =  "查看已下订单之中哪些 没有映射到")
    @PostMapping("/orderEditItem")
    public CommonResult orderEditItem(DknEditOrderDto editOrderDto) {
        try {
            JsonNode jsonNode = dknOrderService.postEditItem(editOrderDto);
            if(UtilValidate.isNotEmpty(jsonNode)){
                String errorDescription = jsonNode.get("error_description").textValue().trim();
                if(UtilValidate.isNotEmpty(errorDescription)){
                    return CommonResult.error("无法修改:"+errorDescription);
                }
            }
            return CommonResult.success(jsonNode);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Schema( name =  "删除订单项")
    @PostMapping("/orderEditRemove")
    public CommonResult orderEditRemove(DknEditOrderDto editOrderDto) {
        try {
            JsonNode jsonNode = dknOrderService.postRemoveItem(editOrderDto);
            if(UtilValidate.isNotEmpty(jsonNode)){
                String errorDescription = jsonNode.get("error_description").textValue().trim();
                if(UtilValidate.isNotEmpty(errorDescription)){
                    return CommonResult.error("无法删除:"+errorDescription);

                }
            }
            return CommonResult.success(jsonNode);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Schema( name =  "新增订单项")
    @PostMapping("/orderEditAdd")
    public CommonResult orderEditAdd(DknEditOrderDto editOrderDto) {
        try {
            JsonNode jsonNode = dknOrderService.postAddItem(editOrderDto);

            return CommonResult.success(jsonNode);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

}
